package cn.dm.server;

import cn.dm.MessageProducer;
import cn.dm.RingBufferWorkerPoolFactory;
import cn.dm.entity.TranslatorData;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class ServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        /**
         * 写法一
         * (与ClientHandler要对应)
         */
//        TranslatorData request = (TranslatorData) msg;
//        System.err.println("Sever端: id= " + request.getId()
//                + ", name= " + request.getName()
//                + ", message= " + request.getMessage());
//        //数据库持久化操作 IO读写 ---> 交给一个线程池 去异步的调用执行
//        TranslatorData response = new TranslatorData();
//        response.setId("resp: " + request.getId());
//        response.setName("resp: " + request.getName());
//        response.setMessage("resp: " + request.getMessage());
//        //写出response响应信息:
//        ctx.writeAndFlush(response);


        /**
         * 写法二(优化)
         * (与ClientHandler要对应)
         */
        TranslatorData request = (TranslatorData) msg;
        //自已的应用服务应该有一个ID生成规则
        String producerId = "code:sessionId:001";
        MessageProducer messageProducer = RingBufferWorkerPoolFactory.getInstance().getMessageProducer(producerId);
        messageProducer.onData(request, ctx);
    }
}
